//
//  CommentViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/16.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "CommentViewController.h"
#import "CommentTableViewCell.h"
#import "CommitStarsView.h"
static NSString *cellIdentify = @"CommentTableViewCell";

@interface CommentViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UILabel *bagNumber;
@property (strong, nonatomic) IBOutlet UILabel *commentNumber;
@property (strong, nonatomic) IBOutlet UIView *dealView;
@property(nonatomic,strong)NSArray * commitArray;
@property(nonatomic,strong)NSMutableArray * heightArray;

@end

@implementation CommentViewController

- (void)viewWillAppear:(BOOL)animated{
    [self getBagNumber];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViewLayOut];
    // Do any additional setup after loading the view.
}

- (void)initViewLayOut{
    
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.tableView];
     [self adaptiveViewLayout:self.dealView];
    self.bagNumber.layer.cornerRadius = 6*VerticalRatio();
    self.bagNumber.layer.masksToBounds = YES;

    NSDictionary * dic = @{@"headImage":@"comment1",@"personName":@"Cherry",@"propertyLabel":@"2014-10-31  尺码：37  颜色分类：黑色",@"imageArray":@[@"comment11",@"comment11",@"comment11"],@"commentArray":@[@"不错",@"穿着很舒服",@"nice"],@"starts":@"3.5"};
    NSDictionary * dic1 = @{@"headImage":@"comment1",@"personName":@"Cherry",@"propertyLabel":@"2014-10-31  尺码：37  颜色分类：黑色",@"imageArray":@[],@"commentArray":@[@"不错",@"穿着很舒服",@"nice"],@"starts":@"4"};
    NSDictionary * dic2 = @{@"headImage":@"comment1",@"personName":@"Cherry",@"propertyLabel":@"2014-10-31  尺码：37  颜色分类：黑色",@"imageArray":@[@"comment11",@"comment11",@"comment11"],@"commentArray":@[@"不错",@"nice"],@"starts":@"4.5"};
    NSDictionary * dic3 = @{@"headImage":@"comment1",@"personName":@"Cherry",@"propertyLabel":@"2014-10-31  尺码：37  颜色分类：黑色",@"imageArray":@[],@"commentArray":@[@"不错",@"nice"],@"starts":@"5"};
    _commitArray = @[dic,dic1,dic2,dic3];
    _heightArray = [NSMutableArray array];
    for (int i = 0; i<_commitArray.count; i++) {
        NSArray * imageArray = _commitArray[i][@"imageArray"];
        NSArray * commitArray = _commitArray[i][@"commentArray"];
        NSInteger height = 0;
        if (imageArray.count ==0 && commitArray.count == 1) {
            height = 120;
        }else if (imageArray.count >0 && commitArray.count == 1){
            height = 170;
        }else{
            for (int j = 0; j<commitArray.count; j++) {
                NSString * comment = commitArray[j];
                int lines = 0;
                CGSize cSize ;
                if (j==0) {
                    cSize = [comment sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
                }else{
                    cSize = [comment sizeWithFont:[UIFont systemFontOfSize:11*VerticalTexeRatio()]];
                }
                
                if (cSize.width<290*VerticalRatio()) {
                    lines=1;
                }else{
                    lines = cSize.width/290*VerticalRatio()+1;
                }
                height+= lines*(cSize.height);
            }
            
            if (imageArray.count ==0 && commitArray.count >1){
                height += 120;
            }else if (imageArray.count >0 && commitArray.count >1){
                height += 170;
            }
        }
        [_heightArray addObject:@(height)];
    }
    
    //注册
    [self.tableView registerClass:[CommentTableViewCell class]  forCellReuseIdentifier:cellIdentify];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
    
    
}
- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];

}
- (IBAction)shoppingBag:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"ShoppingHome"];
    [self.navigationController   pushViewController:page animated:YES];
}


#pragma mark  获取购物袋数量--=========================================
-(void)getBagNumber{
    
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:@"queryCartProductTotalNum" forKey:@"method"];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"userId"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_queryCartProductTotalNum withHttpType:@"POST" withTag:SX_QueryCartProductTotalNum];
}

#pragma mark == 客服
- (IBAction)callWaiter:(id)sender {
}

#pragma mark == 店铺
- (IBAction)shopHandel:(id)sender {
}
#pragma mark == 收藏
- (IBAction)collectHandel:(id)sender {
}

#pragma mark == 加入购物车
- (IBAction)putInBag:(id)sender {
}
#pragma mark == 立即购买
- (IBAction)buyNow:(id)sender {
}


#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify forIndexPath:indexPath];
    if (!cell) {
        cell = [[CommentTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    CommitStarsView * stars = [[CommitStarsView alloc]initWithFrame:flexibleFrame(CGRectMake(15, 47, 80, 15), NO) Stars:[_commitArray[indexPath.row][@"starts"]floatValue]];
    [cell addSubview:stars];
    cell.timeLabel.text =[_commitArray[indexPath.row][@"time"] substringToIndex:18];
    cell.headImage.image = [UIImage imageNamed:_commitArray[indexPath.row][@"headImage"]];
    cell.personName.text =  _commitArray[indexPath.row][@"personName"];
    cell.commentWords.text = _commitArray[indexPath.row][@"commentArray"][0];
    [cell.commentWords sizeToFit];
    
    cell.imageArray = _commitArray[indexPath.row][@"imageArray"];
    cell.commentArray = _commitArray[indexPath.row][@"commentArray"];
    
    
    cell.propertyLabel.frame = flexibleFrame(CGRectMake(15, [_heightArray[indexPath.row] integerValue]-25, 280, 0), NO);
    cell.propertyLabel.text = _commitArray[indexPath.row][@"propertyLabel"];
    [cell.propertyLabel sizeToFit];
    
    
    NSString * comment =cell.commentArray[0];
    CGSize comSize = [comment sizeWithFont:[UIFont systemFontOfSize:14*VerticalTexeRatio()]];
    NSInteger Y;
    if (comSize.width<290*VerticalRatio()) {
        Y=comSize.height;
    }else{
        Y=(comSize.width/290+1)*(comSize.height+10);
    }
    //评论
    if (cell.commentArray.count>1) {
        for (int i = 1; i<cell.commentArray.count; i++) {
            //            NSString * string1 = cell.commentArray[i][@"nickname"];
            //            NSString * string2 = cell.commentArray[i][@"replycontent"];
            //            NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ %@",string1,string2]];
            //            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.153 green:0.204 blue:0.329 alpha:1.000] range:NSMakeRange(0,string1.length)];
            
            UILabel * label = [[UILabel alloc]init];
            if (cell.imageArray.count>0) {
                label.frame = flexibleFrame(CGRectMake(15, 135+Y+(i-1)*17, 290, 15), NO);
            }else{
                label.frame = flexibleFrame(CGRectMake(15, 80+Y+(i-1)*20, 290, 15), NO);
            }
            label.textColor = [UIColor colorWithWhite:0.200 alpha:1.000];
            label.backgroundColor = [UIColor whiteColor];
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:11*VerticalRatio()];
            //            label.attributedText = str;
            label.text = cell.commentArray[i];
            [cell addSubview:label];
        }
        
        
    }
    
    if (cell.imageArray.count>0) {
        
        UIScrollView * scroView = [[UIScrollView alloc]initWithFrame:flexibleFrame(CGRectMake(17, 70+Y, 283, 50), NO)];
        scroView.contentSize = flexibleSize(CGSizeMake(50*cell.imageArray.count+1, 50), NO);
        scroView.showsHorizontalScrollIndicator = NO;
        [cell addSubview:scroView];
        
        
        for (int i = 0; i<cell.imageArray.count; i++) {
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:flexibleFrame(CGRectMake(55*i, 0, 50, 50), NO)];
            //            [imageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,cell.imageArray[i]]]];
            imageView.image = [UIImage imageNamed:cell.imageArray[i]];
            [scroView addSubview:imageView];
        }
    }
    
    UILabel * line = [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0, [_heightArray[indexPath.row] floatValue]-5, 320, 5), NO)];
    line.backgroundColor =[UIColor colorWithWhite:0.898 alpha:1.000];
    [cell addSubview:line];
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _commitArray.count;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger height= [_heightArray[indexPath.row] integerValue];
    return height*VerticalRatio();

}


#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{

    if (tag == SX_QueryCartProductTotalNum){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            _bagNumber.text =[NSString stringWithFormat:@"%@", database[@"result"]];
        }
        
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
